<?php
/**
 * NAME: ops
 * WHEN: OnMsg OnJoin OnLeave OnNick Every15000
 * AUTOLOAD: true
 * DESCRIPTION: Zarządzanie operatorami/administratorami. Funkcja autoop itd. Co 20 minut resetuje tymczasowych operatorow.
 * VERSION: 0.1
 * UPDATE: http://update.tempaq2.blacksoft.eu/check.php
 */

exit();

//*module_register_start
	$cmd = $module->addCommand('!admin', '_lang_admin_desc');
	$module->addSubcommand($cmd, 'test', '_lang_admintest_desc', '[^ ]*');
	$module->addSubcommand($cmd, 'gain', '_lang_gain_op_help', '.+'); #2
	$module->addSubcommand($cmd, 'add', '_lang_add_help', '([^ ]+) (~[^ ]+) ([10]{1})[ ]?([^ ]*)[ ]?(.*)', true);
	$module->addSubcommand($cmd, 'del', '_lang_del_help', '[^ ]+', true); #4
	$module->addSubcommand($cmd, 'edit', '_lang_edit_op_help', null, true); #5
	$module->addSubcommand($cmd, 'list', '_lang_list_help', null, true); #6
	$bot->memo('haslo', OP_PASSWORD);
	$bot->memo('to_kick', array());
	$bot->memo('last_reload', time());
	
//*function_start
	if ($bot->time%(1000*60*5)===0 && ((time()-$bot->memo('last_reload'))>5000)) {
		$bot->reload_ops();
		$bot->memo('last_reload', time());
	} elseif ($bot->time%5000===0) {
		$kogo = $bot->memo('to_kick');
		if (is_array($kogo)) {
			foreach ($kogo as $k=>$v) {
				if ((times() - $v['time'] )>= 29999) {
					$irc->kick($v['channel'], $v['name'], $lang->text('wrong_nick'));
					unset($kogo[$k]);
					$bot->memo('to_kick', array_values($kogo));
				}
			}
		}
	}
	if (isset($data->nick)) {
		$nick = $data->nick;
		if ($data->type === SMARTIRC_TYPE_NICKCHANGE) $data->nick = $data->message;
		if ($data->type === SMARTIRC_TYPE_JOIN || $data->type === SMARTIRC_TYPE_NICKCHANGE) { //ktos wchodzi na kanal
			//sprawdzamy czy nalezy mu dac opa:)
			$x = sqlite::fetchSQL('SELECT * FROM ops WHERE name="'.$data->nick.'"');
			if ($bot->is_admin($data->nick)) {
				if (count($x)>0) {
					foreach ($x as $y)
						if ($y['autoop']=="1") {
							if (isset($data->channel))
								$irc->op($data->channel, $data->nick);
							else {
								foreach ($irc->channel as $c) {
									if (is_object($c) && isset($c->users[strtolower($data->nick)])) {
										//koles jest na kanale - dajemy mu opa
										$irc->op($c->name, $data->nick);
									}
								}
							}
						}
				}
			} elseif (count($x)>0) {
				$bot->send_text('nick_reserved');
				$co = $bot->memo('to_kick');
				if (!is_array($co)) $co = array();
				$co[] = array('name'=>$data->nick, 'channel'=>$data->channel, 'time' => time());
				$bot->memo('to_kick', $co);
			}
			unset($table);
		}
		if ($data->type === SMARTIRC_TYPE_PART || $data->type === SMARTIRC_TYPE_NICKCHANGE) {
			$kogo = $bot->memo('to_kick');
			if (is_array($kogo)) {
				foreach ($kogo as $k => $v) {
					if ($v['name'] == $data->nick && $v['channel'] == $data->channel) {
						unset($kogo[$k]);
						$bot->memo('to_kick', array_values($kogo));
					}
				}
			}
		}
		if ($data->type === SMARTIRC_TYPE_NICKCHANGE) $data->nick = $nick;
	}
	if (is_array($parsed_module) && count($parsed_module)) {
		foreach ($parsed_module as $k=>$v) {
			$single = (isset($v[0])?$v[0]:null);
			switch ($k) {
				case 0:
					break;
				case 1: //!admintest
					if (empty($single)) {
						$nick = $data->nick;
					} else {
						$nick = $single;
					}
					if ((($a = $bot->is_admin($nick))=== false)|| $a) {
						$bot->send($nick.": ".(($a)?"[OK]":"[NO]"));
					} else {
						$bot->send_text('user_is_offline', $a);
					}
					break;
				case 2:
					if ($single === $bot->memo('haslo')) {
						$bot->ops[] = array('name' => $data->nick, 'ident'=>$data->ident, 'host' => $data->host);
						$bot->send_text('access_granted');
					}
					break;
				case 3:
					//$table = new table('ops');
					if (!isset($v[4])) $v[4]="-";
					sqlite::query('INSERT INTO ops (name,ident,autoop,host,pass) VALUES ("'.$v[1].'","'.$v[2].'","'.$v[3].'","'.$v[4].'","'.(isset($v[5])?$v[5]:0).'")');
					//$table->insert(array('name'=>$v[1], 'ident'=>$v[2], 'autoop'=>$v[3], 'host'=>$v[4], 'pass'=>(isset($v[5])?$v[5]:0)));
					$bot->send_text('add_op_success', $v[1]);
					//unset($table);
					break;
				case 4:
					//$table = new table('ops');
					sqlite::query('DELETE FROM ops WHERE name="'.$v[0].'"');
					//if (!$table->delete('name', $v[0])) {
						$bot->send_text('del_op_success');
					//} else $bot->send_text('del_op_not_found', $v[0]);
					//unset($table);
					break;
				case 5:
					$bot->send_text('edit_op_help');
					break;
				case 6:
					$a = $bot->send_text('ops');
					foreach($bot->ops as $x) {
						$a .= $x['name'].' ';
					}
					$bot->send($a);
					break;
				default:
					$bot->send('Not implemented yet');
			}
		}
	}
//*function_end
?>